package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.PurchaseEntity;
import com.softlab.retailwholesalesystem.entity.RepositoryEntity;
import com.softlab.retailwholesalesystem.entity.StoreEntity;
import com.softlab.retailwholesalesystem.mapper.GoodsMapper;
import com.softlab.retailwholesalesystem.mapper.PurchaseMapper;
import com.softlab.retailwholesalesystem.mapper.RepositoryMapper;
import com.softlab.retailwholesalesystem.mapper.StoreMapper;
import com.softlab.retailwholesalesystem.service.IRepositoryService;
import com.softlab.retailwholesalesystem.service.exception.DeleteErrorException;
import com.softlab.retailwholesalesystem.service.exception.InsertRunTimeException;
import com.softlab.retailwholesalesystem.service.exception.RepositoryNameDuplicatedException;
import com.softlab.retailwholesalesystem.service.exception.UpdateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepositoryServiceImpl extends ServiceImpl<RepositoryMapper, RepositoryEntity> implements IRepositoryService {

    @Autowired
    RepositoryMapper mapper;

    @Autowired
    StoreMapper storeMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<RepositoryEntity> getAllRepository() {
        QueryWrapper<RepositoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);

        List<RepositoryEntity> repositoryList = mapper.selectList(wrapper);
        for (RepositoryEntity repository :repositoryList){
            Double unSold = 0.0;
            QueryWrapper<StoreEntity> qw = new QueryWrapper<>();
            qw.eq("is_delete", 0);
            qw.eq("repository_id", repository.getRepositoryId());

            List<StoreEntity> storeList = storeMapper.selectList(qw);

            for (StoreEntity store : storeList){
                unSold += store.getGoodsNumber() * goodsMapper.selectById(store.getGoodsId()).getStockPrice();
            }
            repository.setUnsoldMoney(unSold.toString());

        }


        return repositoryList;
    }

    @Override
    public void addRepository(RepositoryEntity repository) {
        QueryWrapper<RepositoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("repository_name",repository.getRepositoryName());
        wrapper.eq("is_delete",0);
        if (mapper.selectOne(wrapper) == null){
            int i = mapper.insert(repository);
            if(i == 0)
                throw new InsertRunTimeException("插入时错误");
        }
        else
            throw new RepositoryNameDuplicatedException("仓库名已存在");
    }

    @Override
    public void deleteRepository(RepositoryEntity repository) {
        repository.setIsDelete(1);
        int i = mapper.updateById(repository);
        if (i==0){
            throw new DeleteErrorException("删除时遇到未知错误");
        }
    }

    @Override
    public void changeRepository(RepositoryEntity repository) {
        int i = mapper.updateById(repository);
        if (i == 0){
            throw new UpdateErrorException("新增时出现异常");
        }
    }

    @Override
    public List<RepositoryEntity> getRepository(RepositoryEntity repository1) {
        QueryWrapper<RepositoryEntity> wrapper = new QueryWrapper<>();
        if (repository1.getRepositoryName() != null) wrapper.eq("repository_name", repository1.getRepositoryName());
        if (repository1.getRepositoryId() != null) wrapper.eq("repository_id", repository1.getRepositoryId());
        wrapper.eq("is_delete",0);


        List<RepositoryEntity> repositoryList = mapper.selectList(wrapper);
        for (RepositoryEntity repository :repositoryList){
            Double unSold = 0.0;
            QueryWrapper<StoreEntity> qw = new QueryWrapper<>();
            qw.eq("is_delete", 0);
            qw.eq("repository_id", repository.getRepositoryId());

            List<StoreEntity> storeList = storeMapper.selectList(qw);

            for (StoreEntity store : storeList){
                unSold += store.getGoodsNumber() * goodsMapper.selectById(store.getGoodsId()).getStockPrice();
            }
            repository.setUnsoldMoney(unSold.toString());

        }

        return repositoryList;
    }

}
